<?php
namespace App\Services;

use App\Models\ArticleCategory;
use App\Models\Article;
use Illuminate\Support\Facades\Input;

/**
 *
 * 文章服务层
 *
 * @author maqu
 *
 */
class ArticleService extends BaseService {

    public function getArticlesByCateId($cate_id){

        $firstrow = Input::get('firstrow',0);
        $pagesize = Input::get('pagesize',10);

        $articles = Article::where('if_show',YES)
            ->where('cate_id',$cate_id)
            ->orderBy('sort_order','asc')
            ->orderBy('add_time','desc')
            ->select('article_id','title','logo','intro')
            ->skip($firstrow)->take($pagesize)
            ->get();

        foreach($articles as $article){
            $article->logo= complete_url($article->logo);
        }
        return $articles->toArray();
    }

    public function getDetailByArticleId($article_id){
        $article =Article::find($article_id);
        if($article){
            $article->logo = complete_url($article->logo);
            $article->content=complete_html(config('maqu.STATIC_RESOURCE_URL'),$article->title,$article->content);
            return $this->success($article->toArray());
        } else {
            return $this->failure('该文章不存在');
        }
    }

    public function getDetailByArticleCode($article_code){
        $article =Article::where('code',$article_code)->first();
        if($article){

            $article->logo = complete_url($article->logo);
            $article->content=complete_html(config('maqu.STATIC_RESOURCE_URL'),$article->title,$article->content);
            return $this->success($article->toArray());
        } else {
            return $this->failure('该文章不存在');
        }
    }

    public function getChildrenCategorys($parent_id=0){

        $categorys = ArticleCategory::where('parent_id',$parent_id)
            ->orderBy('sort_order','asc')
            ->select('cate_id','cate_name','code')
            ->get();

        return $categorys->toArray();
    }

    public function convertCateCodetoCateId($code){
        $cate =ArticleCategory::where('code',$code)->first();
        if($cate){
            return $this->success($cate->cate_id);
        } else {
            return $this->failure('文章分类Code不才存在');
        }
    }

}